//
//  LS_StatusCell.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/24.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit
class LS_StatusCell: UITableViewCell {
    //声明toolBar上部的约束属性
    var toolBarTopCons:Constraint?
    //模型属性
    var statusViewModel:LS_StatueViewModel?{
        didSet{
        statusOriginalView.statusViewModel = statusViewModel
        statusToolBar.statusViewModel = statusViewModel
            //判断是否是原创微博
            //卸载工具条的顶部约束对象
            toolBarTopCons?.uninstall()
            if statusViewModel?.status?.retweeted_status == nil {
                //是原创微博
                //设置数据
                statusRetweetedView.statusViewModel = nil
                //隐藏转发微博视图
                statusRetweetedView.hidden = true
                //更新约束 底部工具条的顶部相对于原创微博的底部添加约束
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopCons = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            }else{
                //转发微博
                //设置数据
                statusRetweetedView.statusViewModel = statusViewModel
                //显示转发微博视图
                statusRetweetedView.hidden = false
                //更新约束 底部工具条的顶部相对于转发微博的底部添加约束
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                })
            }

        }
    }

    override func awakeFromNib() {//从xib加载cell会调用这个方法
        super.awakeFromNib()
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置背景颜色
//        self.backgroundColor = randomColor()
        //设置选中模式
        self.selectionStyle = .None
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUI(){
    //自定义cell添加视图不能够直接使用cell添加 需要使用cell的contentView添加子视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(statusRetweetedView)
        //设置原创微博视图约束
        statusOriginalView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self.contentView)
            //TODO:
        }
        //设置转发微博视图的约束
        statusRetweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(self.contentView)
        }
        //设置底部工具条视图约束
        statusToolBar.snp_makeConstraints { (make) in
//           self.tollBarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            self.toolBarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.left.right.equalTo(self.contentView)
            //设置底部工具条的高度
            make.height.equalTo(36)
        }
        //给cell的contentView的底部添加约束
        //snp --> 取消了视图的frame 布局
        contentView.snp_makeConstraints { (make) in
            //将contentView的约束都加上
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
        }

    }
    
    
    
    //MARK:懒加载子视图
    //懒加载原创微博视图
    private lazy var statusOriginalView: LS_StatusOriginalView = LS_StatusOriginalView()
    //底部工具条
    private lazy var statusToolBar: LS_StatusToolBar = LS_StatusToolBar()
    //转发微博视图
    private lazy var statusRetweetedView: LS_StatusRetweetedView = LS_StatusRetweetedView()
}
